<template>
  <div class="article-create">
    <el-card>
      <el-input
        v-model="title"
        class="title-input"
        :placeholder="$t('msg.article.titlePlaceholder')"
        maxlength="20"
        clearable
      />
      <el-tabs v-model="activeName">
        <el-tab-pane :label="$t('msg.article.markdown')" name="markdown">
          <markdown :title="title" :detail="detail" @on-success="onSuccess" />
        </el-tab-pane>
        <el-tab-pane :label="$t('msg.article.richText')" name="editor">
          <editor :title="title" :detail="detail" @on-success="onSuccess" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
  import Editor from './components/Editor.vue'
  import Markdown from './components/Markdown.vue'
  import { ref } from 'vue'
  import { useRoute } from 'vue-router'
  import { articleDetail } from '@/api/article'

  // 处理编辑相关
  const route = useRoute()
  const articleId = route.params.id
  const detail = ref({})
  const getArticleDetail = async () => {
    detail.value = await articleDetail(articleId)
    // 标题赋值
    title.value = detail.value.title
  }
  if (articleId) {
    getArticleDetail()
  }

  const activeName = ref('markdown')
  const title = ref('')

  // 创建成功
  const onSuccess = () => {
    title.value = ''
  }
</script>

<style lang="scss" scoped>
  .title-input {
    margin-bottom: 20px;
  }
</style>
